﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogisticsBLL;
using System.Text.RegularExpressions;
using System.Data.SqlClient;

namespace WebLosgic.Accounting
{
    public partial class UpdateCost : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRptList();
            }
        }

        public bool BtnState(string findState)
        {
            bool vis = false;
            if (findState == "2")
            {
                vis = true;
            }
            else
            {
                vis = false;
            }
            return vis;
        }

        public bool BtnState2(string findState)
        {
            bool vis = false;
            if (findState == "3")
            {
                vis = true;
            }
            else
            {
                vis = false;
            }
            return vis;
        }

        public void BindRptList()
        {
            CostBLL cb = new CostBLL();
            int pageIndex = AspNetPagerTool.CurrentPageIndex;
            int pageSize = AspNetPagerTool.PageSize;
            int Rescout = 0;
            int state = Convert.ToInt32(RblSUMState.SelectedValue);
            string ReceiveDate = TxtChenDate.Text;
            string EndReceDate = Txtchengdata.Text.Trim();
            RptBindDrive.DataSource = cb.CostCarries(pageIndex, pageSize, out Rescout, state, ReceiveDate, EndReceDate);
            AspNetPagerTool.RecordCount = Rescout;
            RptBindDrive.DataBind();
        }

        protected void BtnUpdate_Click(object sender, EventArgs e)
        {
            CostBLL cb = new CostBLL();
            string Zhen = @"^[0-9]\d{0,7}(\.\d{1,6})?$";
            Regex rex = new Regex(Zhen);
            if (rex.IsMatch(TextInsuranceCost.Text) == false ||
                rex.IsMatch(TextTransportCost.Text.Trim()) == false ||
                rex.IsMatch(TextFlie.Text.Trim()) == false ||
                rex.IsMatch(TextOtherCost.Text.Trim()) == false
                )
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('只能是数字或小数点类型')", true);
            }
            else
            {
                int SchedulingID = Convert.ToInt32(Session["Cids"]);
                float Oilfolt = Convert.ToSingle(TextInsuranceCost.Text);
                float Toll = Convert.ToSingle(TextTransportCost.Text);
                float Fine = Convert.ToSingle(TextFlie.Text);
                float Other = Convert.ToSingle(TextOtherCost.Text);
                int result = cb.Update(SchedulingID, Oilfolt, Toll, Fine, Other);

                string name = Session["names"].ToString();
                string Behavor = "" + name + ": 修改成本";
                int TypeId = 19;
                int UserID = Convert.ToInt32(Session["UserIDs"]);
                string Parameters = @"" + Oilfolt + "," + Toll + "," + Fine + "," + Other;
                string ProcName = "P_UpdateCost";
                HttpRequest request = HttpContext.Current.Request;
                string IP = "192.168.0.1";
                int IsExcePion = 0;

                if (result > 0)
                {
                    IsExcePion = 0;
                    int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                    if (a > 0)
                    {
                        Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('修改成功')", true);
                        divNewBlock.Style.Add("display", "none");
                        BindRptList();
                    }


                }
                else
                {
                    IsExcePion = 1;
                    int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                    if (a > 0)
                    {
                        Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('修改失败')", true);
                        divNewBlock.Style.Add("display", "none");
                        BindRptList();
                    }
                }
            }
        }

        protected void BtnOperation_Click(object sender, EventArgs e)
        {
            CostBLL cb = new CostBLL();
            int carrid = Convert.ToInt32(Session["Cid"]);
            
            string Zhen = @"^[0-9]\d{0,7}(\.\d{1,6})?$";
            Regex rex = new Regex(Zhen);
            if (rex.IsMatch(TextInsuranceCost.Text) == false ||
                rex.IsMatch(TextTransportCost.Text.Trim()) == false ||
                rex.IsMatch(TextFlie.Text.Trim()) == false ||
                rex.IsMatch(TextOtherCost.Text.Trim()) == false
                )
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('输入的值必须是小数点类型，保留两位小数点')", true);
            }
            else
            {
                float Oilfolt = Convert.ToSingle(TextInsuranceCost.Text);
                float Toll = Convert.ToSingle(TextTransportCost.Text);
                float Fine = Convert.ToSingle(TextFlie.Text);
                float Other = Convert.ToSingle(TextOtherCost.Text);
                int result = cb.Cost(Oilfolt, Toll, Fine, Other,carrid);
                string name = Session["names"].ToString();
                string Behavor = "" + name + ": 结算承运单";
                int TypeId = 18;
                int UserID = Convert.ToInt32(Session["UserIDs"]);
                string Parameters = @"" + Oilfolt + "," + Toll + "," + Fine + "," + Other;
                string ProcName = "p_InsertCost";
                HttpRequest request = HttpContext.Current.Request;
                string IP = "192.168.0.1";
                int IsExcePion = 0;


                if (result > 0)
                {
                    IsExcePion = 0;
                    int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                    if (a > 0)
                    {
                        Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('结算成功')", true);
                        divNewBlock.Style.Add("display", "none");
                        BindRptList();
                    }

                }
                else
                {
                    IsExcePion = 1;
                    int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                    if (a > 0)
                    {
                        Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('结算失败')", true);
                        divNewBlock.Style.Add("display", "none");
                        BindRptList();
                    }

                }
            }
        }

        protected void BtnSelect_Click(object sender, EventArgs e)
        {
            divNewBlock.Style.Add("display", "none");
            CostBLL cb = new CostBLL();
            int pageIndex = AspNetPagerTool.CurrentPageIndex;
            int pageSize = AspNetPagerTool.PageSize;
            int Rescout = 0;
            int state = Convert.ToInt32(RblSUMState.SelectedValue);
            string ReceiveDate = TxtChenDate.Text;
            string EndReceDate = Txtchengdata.Text.Trim();
            RptBindDrive.DataSource = cb.CostCarries(pageIndex, pageSize, out Rescout, state, ReceiveDate, EndReceDate);
            AspNetPagerTool.RecordCount = Rescout;
            RptBindDrive.DataBind();
        }

        protected void BtnReset_Click(object sender, EventArgs e)
        {
            RblSUMState.SelectedValue = "-1";
            TxtChenDate.Text = "";
            Txtchengdata.Text = "";
        }

        protected void AspNetPagerTool_PageChanged(object sender, EventArgs e)
        {
            BindRptList();
        }

       

        public void BindDivDisplay()
        {
            CostBLL cb = new CostBLL();
            int carrid = Convert.ToInt32(Session["Cids"]);
            SqlDataReader read = cb.BindCostRead(carrid);
            if (read.Read())
            {
                TextInsuranceCost.Text = read["OilCost"].ToString();
                TextTransportCost.Text = read["Toll"].ToString();
                TextFlie.Text = read["Fine"].ToString();
                TextOtherCost.Text = read["OtherCost"].ToString();
            }

        }


        public void Getbind()
        {
            TextInsuranceCost.Text = "0.00";
            TextTransportCost.Text = "0.00";
            TextFlie.Text = "0.00";
            TextOtherCost.Text = "0.00";

        }

        protected void RptBindDrive_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName== "Update")
            {
                int SchedulingID = Convert.ToInt32(e.CommandArgument);
                divNewBlock.Style.Add("display", "block");
                Session["Cids"] = SchedulingID.ToString();
                BindDivDisplay();
                BtnUpdate.Visible = true;
                BtnOperation.Visible = false;

            }
            if (e.CommandName == "Jiesuan")
            {
                int SchedulingID = Convert.ToInt32(e.CommandArgument);
                divNewBlock.Style.Add("display", "block");
                Session["Cid"] = SchedulingID.ToString();
                Getbind();
                BtnOperation.Visible = true;
                BtnUpdate.Visible = false;
            }
        }
    }
}